package com.example.demo.demos.util;

import com.example.demo.demos.domain.vo.DataRows;

import java.util.ArrayList;
import java.util.List;

public class ListComparator {

    /**
     * 对比两个字符串列表，检查每一行每一列是否有值相等
     * @param list1 第一个列表
     * @param list2 第二个列表
     * @return 对比结果，包含每个位置的比较信息
     */
    public static List<List<ComparisonResult>> compareLists(List<List<String>> list1, List<List<String>> list2) {
        // 确定最大行数和最大列数
        int maxRows = Math.max(list1.size(), list2.size());
        List<List<ComparisonResult>> results = new ArrayList<>();

        for (int row = 0; row < maxRows; row++) {
            List<String> row1 = row < list1.size() ? list1.get(row) : null;
            List<String> row2 = row < list2.size() ? list2.get(row) : null;

            int maxCols = 0;
            if (row1 != null) {
                maxCols = Math.max(maxCols, row1.size());
            }
            if (row2 != null) {
                maxCols = Math.max(maxCols, row2.size());
            }

            List<ComparisonResult> rowResults = new ArrayList<>();

            for (int col = 0; col < maxCols; col++) {
                String value1 = (row1 != null && col < row1.size()) ? row1.get(col) : null;
                String value2 = (row2 != null && col < row2.size()) ? row2.get(col) : null;

                boolean equals = false;
                if (value1 == null && value2 == null) {
                    equals = true;
                } else if (value1 != null && value2 != null) {
                    equals = value1.equals(value2);
                }

                rowResults.add(new ComparisonResult(value1, value2, equals));
            }

            results.add(rowResults);
        }

        return results;
    }

    /**
     * 打印对比结果
     * @param results 对比结果列表
     */
    public static List<DataRows> printComparisonResults(List<List<ComparisonResult>> results) {
        StringBuffer stringBuffer = new StringBuffer();

        List<DataRows> dataRows = new ArrayList<>();
        for (int row = 0; row < results.size(); row++) {
            List<ComparisonResult> rowResults = results.get(row);

            for (int col = 0; col < rowResults.size(); col++) {
                DataRows dataRowsSingle = new DataRows();
                ComparisonResult result = rowResults.get(col);
                String status = result.isEqual() ? "相等" : "不相等";
                stringBuffer.append("对比结果 (行号: 列号 - 值1 vs 值2 [是否相等])");
                dataRowsSingle.setLine(row+1);
                dataRowsSingle.setRow(col+1);
                dataRowsSingle.setLineNum(result.getValue1() != null ? result.getValue1() : "null");
                dataRowsSingle.setRowNum(result.getValue2() != null ? result.getValue2() : "null");
                dataRowsSingle.setMsg(status);
                dataRows.add(dataRowsSingle);
//                System.out.printf("行 %d, 列 %d: %s vs %s [%s]%n",
//                        row + 1, col + 1,
//                        result.getValue1() != null ? result.getValue1() : "null",
//                        result.getValue2() != null ? result.getValue2() : "null",
//                        status);
            }
        }
        return dataRows;
    }

    /**
     * 对比结果的内部类
     */
    public static class ComparisonResult {
        private String value1;
        private String value2;
        private boolean isEqual;

        public ComparisonResult(String value1, String value2, boolean isEqual) {
            this.value1 = value1;
            this.value2 = value2;
            this.isEqual = isEqual;
        }

        public String getValue1() {
            return value1;
        }

        public String getValue2() {
            return value2;
        }

        public boolean isEqual() {
            return isEqual;
        }
    }
}
